﻿namespace WMS6.Web.Outbound
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.OutboundService2Ref;

    public class VanningDetail : BasePage
    {
        protected Button btnGoBack;
        protected Button btnSave;
        protected HtmlForm F7543;
        protected GridView gvVanningDetail;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnOrderID;
        protected HtmlInputHidden hdnOwnerID;
        protected HtmlInputHidden hdnType;
        protected HtmlInputHidden hdnVanningID;
        protected HtmlHead Head1;
        protected HtmlImage imgContainerType;
        protected Label lblAllocateRuleDetail;
        protected Label lblBarcode;
        protected Label lblContainerID;
        protected Label lblContainerNO;
        protected Label lblContainerType;
        protected Label lblHeight;
        protected Label lblLength;
        protected Label lblMsg;
        protected Label lblQty;
        protected Label lblRemark;
        protected Label lblSealNO;
        protected Label lblSkuID;
        protected Label lblTotalQty;
        protected Label lblVolume;
        protected Label lblWeight;
        protected Label lblWidth;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtBarcode;
        protected TextBox txtContainerID;
        protected TextBox txtContainerNO;
        protected TextBox txtContainerType;
        protected TextBox txtHeight;
        protected TextBox txtLength;
        protected TextBox txtQty;
        protected TextBox txtRemark;
        protected TextBox txtSealNO;
        protected TextBox txtSkuID;
        protected TextBox txtTotalQty;
        protected TextBox txtVolume;
        protected TextBox txtWeight;
        protected TextBox txtWidth;

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(string.Format("Vanning.aspx?from={0}&ID={1}&detail=1", base.UrlEncode(this.hdnFrom.Value), base.UrlEncode(this.hdnVanningID.Value)));
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtContainerID.Text))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7600_002");
            }
            else if (string.IsNullOrEmpty(this.txtContainerType.Text))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7600_003");
            }
            else if (this.hdnType.Value == "1")
            {
                this.UpdateData();
            }
            else
            {
                this.InserData();
            }
        }

        private VanningDetailInfo GetVanningDetailInfo()
        {
            VanningDetailInfo vanningDetailInfo = new VanningDetailInfo {
                VanningID = this.hdnVanningID.Value,
                ContainerID = this.txtContainerID.Text,
                ContainerType = this.txtContainerType.Text
            };
            if (!string.IsNullOrEmpty(this.txtVolume.Text))
            {
                vanningDetailInfo.Volume = decimal.Parse(this.txtVolume.Text);
            }
            else
            {
                vanningDetailInfo.Volume = 0M;
            }
            if (!string.IsNullOrEmpty(this.txtWeight.Text))
            {
                vanningDetailInfo.Weight = decimal.Parse(this.txtWeight.Text);
            }
            else
            {
                vanningDetailInfo.Weight = 0M;
            }
            if (!string.IsNullOrEmpty(this.txtWidth.Text))
            {
                vanningDetailInfo.Width = decimal.Parse(this.txtWidth.Text);
            }
            else
            {
                vanningDetailInfo.Width = 0M;
            }
            if (!string.IsNullOrEmpty(this.txtLength.Text))
            {
                vanningDetailInfo.Length = decimal.Parse(this.txtLength.Text);
            }
            else
            {
                vanningDetailInfo.Length = 0M;
            }
            if (!string.IsNullOrEmpty(this.txtHeight.Text))
            {
                vanningDetailInfo.Height = decimal.Parse(this.txtHeight.Text);
            }
            else
            {
                vanningDetailInfo.Height = 0M;
            }
            vanningDetailInfo.Remark = this.txtRemark.Text;
            return vanningDetailInfo;
        }

        protected void gvVanningDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvVanningDetail.PageIndex * this.gvVanningDetail.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
            }
        }

        private void InserData()
        {
            List<VanningDetailInfo> vanningDetailList = new List<VanningDetailInfo>();
            VanningDetailInfo vanningDetailInfo = this.GetVanningDetailInfo();
            vanningDetailInfo.Qty = 0M;
            vanningDetailInfo.TotalQty = 0M;
            vanningDetailInfo.SkuID = "";
            vanningDetailInfo.UomID = "";
            vanningDetailInfo.OrderLineID = "0";
            vanningDetailInfo.OwnerID = this.hdnOwnerID.Value;
            vanningDetailInfo.MultiFlag = "Y";
            vanningDetailInfo.IsActive = "N";
            vanningDetailInfo.Status = "OK";
            vanningDetailInfo.CreatedBy = base.UserInfo.UserID;
            vanningDetailInfo.ExternOrderID = "";
            vanningDetailInfo.ExternLineID = "";
            vanningDetailInfo.ContainerNo = this.txtContainerNO.Text;
            vanningDetailInfo.SealNo = this.txtSealNO.Text;
            vanningDetailList.Add(vanningDetailInfo);
            if (vanningDetailList.Count > 0)
            {
                try
                {
                    this.lblMsg.Text = string.Format(base.GetGlobalMsgResource("F7600_004"), OutboundService2ClientProxy.GetInstance().InsertVanningDetail(vanningDetailList));
                    this.LoadData();
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                }
            }
        }

        private void LoadData()
        {
            this.hdnFrom.Value = base.Request.QueryString["from"];
            this.hdnVanningID.Value = base.Request.QueryString["VanningID"];
            if (!string.IsNullOrEmpty(base.Request.QueryString["ContainerID"]) || !string.IsNullOrEmpty(this.txtContainerID.Text))
            {
                if (!string.IsNullOrEmpty(base.Request.QueryString["ContainerID"]))
                {
                    this.txtContainerID.Text = base.Request.QueryString["ContainerID"];
                }
                if (!string.IsNullOrEmpty(base.Request.QueryString["CType"]))
                {
                    this.txtContainerType.Text = base.Request.QueryString["CType"];
                }
                this.hdnType.Value = "1";
                this.textEnabledFalse();
                List<VanningDetailInfo> vanningDetailList = OutboundService2ClientProxy.GetInstance().GetVanningDetailByID(this.hdnVanningID.Value, this.txtContainerID.Text);
                if ((vanningDetailList != null) && (vanningDetailList.Count > 0))
                {
                    foreach (VanningDetailInfo info in vanningDetailList)
                    {
                        if (info.IsActive == "N")
                        {
                            this.txtContainerType.Text = info.ContainerType;
                            this.txtTotalQty.Text = base.GetPreferenceDigit(info.TotalQty);
                            this.txtRemark.Text = info.Remark;
                            this.txtHeight.Text = base.GetPreferenceDigit(info.Height);
                            this.txtLength.Text = base.GetPreferenceDigit(info.Length);
                            this.txtVolume.Text = base.GetPreferenceDigit(info.Volume);
                            this.txtWeight.Text = base.GetPreferenceDigit(info.Weight);
                            this.txtWidth.Text = base.GetPreferenceDigit(info.Width);
                            this.txtContainerNO.Text = info.ContainerNo;
                            this.txtSealNO.Text = info.SealNo;
                        }
                        else
                        {
                            info.VanningQty = (info.TotQty - info.VannedQty) + info.Qty;
                        }
                    }
                    vanningDetailList.RemoveAt(vanningDetailList.Count - 1);
                }
                base.BindData2GridView(this.gvVanningDetail, vanningDetailList);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.lblMsg.Text = "";
        }

        private void textEnabledFalse()
        {
            this.txtContainerID.Enabled = false;
            this.txtContainerType.Enabled = false;
            this.txtTotalQty.Enabled = false;
            this.txtHeight.Enabled = false;
            this.txtLength.Enabled = false;
            this.txtVolume.Enabled = false;
            this.txtWeight.Enabled = false;
            this.txtWidth.Enabled = false;
            this.imgContainerType.Visible = false;
        }

        private void UpdateData()
        {
            List<VanningDetailInfo> vanningDetailList = new List<VanningDetailInfo>();
            VanningDetailInfo updateVanningDetailInfo = this.GetVanningDetailInfo();
            updateVanningDetailInfo.Qty = 0M;
            updateVanningDetailInfo.TotalQty = 0M;
            updateVanningDetailInfo.SkuID = "";
            updateVanningDetailInfo.UomID = "";
            updateVanningDetailInfo.OrderLineID = "0";
            updateVanningDetailInfo.OwnerID = this.hdnOwnerID.Value;
            updateVanningDetailInfo.MultiFlag = "Y";
            updateVanningDetailInfo.IsActive = "N";
            updateVanningDetailInfo.Status = "OK";
            updateVanningDetailInfo.CreatedBy = base.UserInfo.UserID;
            updateVanningDetailInfo.ExternOrderID = "";
            updateVanningDetailInfo.ExternLineID = "";
            updateVanningDetailInfo.ContainerNo = this.txtContainerNO.Text;
            updateVanningDetailInfo.SealNo = this.txtSealNO.Text;
            vanningDetailList.Add(updateVanningDetailInfo);
            int i = 1;
            foreach (GridViewRow row in this.gvVanningDetail.Rows)
            {
                if (!string.IsNullOrEmpty(((TextBox) row.Cells[5].FindControl("txtQty")).Text))
                {
                    VanningDetailInfo vanningDetailInfo = this.GetVanningDetailInfo();
                    vanningDetailInfo.Qty = Convert.ToDecimal(((TextBox) row.Cells[5].FindControl("txtQty")).Text);
                    vanningDetailInfo.TotalQty = 0M;
                    vanningDetailInfo.SkuID = row.Cells[2].Text;
                    vanningDetailInfo.UomID = "";
                    vanningDetailInfo.VannindLineID = i * 10;
                    vanningDetailInfo.OwnerID = this.hdnOwnerID.Value;
                    vanningDetailInfo.MultiFlag = "Y";
                    vanningDetailInfo.IsActive = "Y";
                    vanningDetailInfo.Status = "OK";
                    vanningDetailInfo.CreatedBy = base.UserInfo.UserID;
                    vanningDetailInfo.ExternOrderID = "";
                    vanningDetailInfo.ExternLineID = "";
                    vanningDetailInfo.ContainerNo = this.txtContainerNO.Text;
                    vanningDetailInfo.SealNo = this.txtSealNO.Text;
                    vanningDetailInfo.OrderID = ((Label) row.Cells[7].FindControl("lblOrderID")).Text;
                    vanningDetailInfo.OrderLineID = ((Label) row.Cells[8].FindControl("lblOrderLineID")).Text;
                    vanningDetailInfo.PickDetailID = ((Label) row.Cells[9].FindControl("lblPickDetailID")).Text;
                    vanningDetailInfo.Lot = ((Label) row.Cells[10].FindControl("lblLot")).Text;
                    vanningDetailInfo.Loc = ((Label) row.Cells[11].FindControl("lblLoc")).Text;
                    vanningDetailInfo.LpnID = ((Label) row.Cells[12].FindControl("lblLpnID")).Text;
                    vanningDetailList.Add(vanningDetailInfo);
                    i++;
                }
            }
            if (vanningDetailList.Count > 0)
            {
                try
                {
                    this.lblMsg.Text = string.Format(base.GetGlobalMsgResource("F7600_005"), OutboundService2ClientProxy.GetInstance().UpdateVanningDetail(vanningDetailList));
                    this.LoadData();
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                }
            }
        }
    }
}

